const {
    to
} = require('await-to-js')
const {
    verifyToken
} = require('../utils/tokenUtils')
const {
    FAILED
} = require('../utils/contants')


module.exports = async (req, res, next) => {
    if (req.method === 'OPTIONS') {
        next()
        return
    }
    const [err, userId] = await to(verifyToken(req.headers['token']))
    if (err) {
        res.json({
            code: 401,
            message: 'token非法或过期'
        })
    } else {
        req.userId = userId
        next()
    }
}